package com.bucg.cjcloud.gb28181.sip.xmlbean;

import cn.hutool.core.util.XmlUtil;
import com.bucg.cjcloud.gb28181.common.entity.ItemEntity;
import org.springframework.util.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.xpath.XPathConstants;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class XmlUtils {
    public static String getCmdType(String source) {
        Document docResult = XmlUtil.readXML(source);
        String type = XmlUtil.getByXPath("//Notify/CmdType", docResult, XPathConstants.STRING).toString();
        if (StringUtils.isEmpty(type))
            type = XmlUtil.getByXPath("//Response/CmdType", docResult, XPathConstants.STRING).toString();
        return type.toUpperCase();
    }

    public static String getDeviceId(String source) {
        Document docResult = XmlUtil.readXML(source);
        String deviceid = XmlUtil.getByXPath("//Notify/DeviceID", docResult, XPathConstants.STRING).toString();
        return deviceid;
    }

    public static String getDeviceId(String source, String experssion) {
        Document docResult = XmlUtil.readXML(source);
        String deviceid = XmlUtil.getByXPath(experssion, docResult, XPathConstants.STRING).toString();
        return deviceid;
    }

    /**
     * <?xml version="1.0" encoding="utf-8"?>
     *
     * <Response>
     *   <CmdType>Catalog</CmdType>
     *   <SN>248</SN>
     *   <DeviceID>34020000001320000011</DeviceID>
     *   <SumNum>1</SumNum>
     *   <DeviceList Num="1">
     *     <ItemEntity>
     *       <DeviceID>34020000001320000001</DeviceID>
     *       <Name>Camera 01</Name>
     *       <Manufacturer>Hikvision</Manufacturer>
     *       <Model>IP Camera</Model>
     *       <Owner>Owner</Owner>
     *       <CivilCode>3402000000</CivilCode>
     *       <Address>Address</Address>
     *       <Parental>0</Parental>
     *       <ParentID>34020000002000000001</ParentID>
     *       <SafetyWay>0</SafetyWay>
     *       <RegisterWay>1</RegisterWay>
     *       <Secrecy>0</Secrecy>
     *       <Status>ON</Status>
     *     </ItemEntity>
     *   </DeviceList>
     * </Response>
     * @param source
     * @return
     */

   /* public static String getDeviceList(String source) {
        Document docResult = XmlUtil.readXML(source);
        NodeList item = docResult.getElementsByTagName("DeviceList");
        int length = item.getLength();
        Node item1 = item.item(0);
        NodeList childNodes = item1.getChildNodes();
        Node item2 = childNodes.item(0).getChildNodes().item(0);
        System.out.println(item2.getNodeName());
        String deviceid = XmlUtil.getByXPath("//Response/DeviceList//ItemEntity//DeviceID", docResult, XPathConstants.STRING).toString();
        return deviceid;
    }
    */

    /**
     * 解析Item
     * @param
     * @return
     */
    public static List<ItemEntity> getDeviceList(String source) {
        Document doc = XmlUtil.readXML(source);

        Field[] fields2 = ItemEntity.class.getDeclaredFields();
        NodeList links = doc.getElementsByTagName("Item");
        int length = links.getLength();
        List<ItemEntity> list = new ArrayList<>(length);

        for (int i = 0; i < length; i++) {
            Node node = links.item(i);
            NodeList childNodes = node.getChildNodes();
            ItemEntity item = new ItemEntity();
            for (int j = 0; j < childNodes.getLength(); j++) {
                if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
                    String nodeName = childNodes.item(j).getNodeName();
                    String nodeValue = childNodes.item(j).getFirstChild().getNodeValue();
                    try {
                        for (Field f2 : fields2) {
                            if (nodeName.equalsIgnoreCase(f2.getName())) {
                                //设置访问权限
                                f2.setAccessible(true);
                                f2.set(item, nodeValue);
                            }
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
            list.add(item);
        }
        return list;
    }
}
